package com.xy.modular.sys.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.pager.SearchCondition;
import com.xy.modular.sys.entity.column.ColumnsTable;
import com.xy.modular.sys.service.IColumnsTableService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * Created by BCK on 2019/08/07.
 */
@Controller
@RequestMapping("/columns/table/")
public class ColumnsTableController extends BaseController {

	@Resource
	IColumnsTableService columnsTableService;

	@RequestMapping("save")
	@ResponseBody
	public MReturnObject add(ColumnsTable columnsTable) {
		columnsTableService.save(columnsTable);
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequestMapping("delete")
	@ResponseBody
	public MReturnObject delete(@RequestParam String id) {
		columnsTableService.deleteById(id);
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequestMapping("update")
	@ResponseBody
	public MReturnObject update(ColumnsTable columnsTable) {
		columnsTableService.update(columnsTable);
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequestMapping("list/{tableId}")
	@ResponseBody
	public MReturnObject list(@PathVariable("tableId") String tableId, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addAndCondition(ColumnsTable.TABLEID, SearchCondition.EQUAL, tableId);
		pageRequest.addSortConditions(ColumnsTable.SORT, pageRequest.ORDER_ASC);
		return new MReturnObject(MReturnObject.SUCCESS, columnsTableService.findAll(pageRequest));
	}

	@RequestMapping("by/{tableId}")
	@ResponseBody
	public MReturnObject by(HttpServletRequest request,@PathVariable("tableId") String tableId) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addAndCondition(ColumnsTable.TABLEID, SearchCondition.EQUAL, tableId);
		pageRequest.addSortConditions(ColumnsTable.SORT, pageRequest.ORDER_ASC);
		return new MReturnObject(MReturnObject.SUCCESS, columnsTableService.findAll(pageRequest));
	}
}
